import {
  ADD_GOODS,
  INIT_SHOP_CART,
  REDUCE_GOODS,
  SINGLE_SELECT_GOODS,
  ALL_SELECT_GOODS,
  DELETE_SELECT_GOODS,
  USER_INFO,
  INIT_USER_INFO,
  CHANGE_USER_NICK_NAME,
  USER_INFO_BRITHDAY,
  USER_INFO_SEX,
  LOGIN_OUT,
  INIT_USER_SHOPPING_ADDRESS,
  ADD_USER_SHOPPING_ADDRESS,
  DELETE_USER_SHOPPING_ADDRESS,
  CHANGE_USER_SHOPPING_ADDRESS
} from './mutation-types'

import {
  getLocalStore,
  setLocalStore,
  removeLocalStore
} from '../config/global'
import {
  Toast
} from 'vant'

import { ADD_TO_CART } from '../config/pubsub-type'
import Vue from 'vue'
import Router from '@/router/index.js'

export default {
  // 添加商品
  [ADD_GOODS] (state, { goodsID, goodsName, smallImage, goodsPrice }) {
    const shopCart = state.shopCart
    if (shopCart[goodsID]) {
      shopCart[goodsID]['num']++
    } else {
      shopCart[goodsID] = {
        num: 1,
        id: goodsID,
        price: goodsPrice,
        smallImage: smallImage,
        name: goodsName,
        checked: true
      }
    }
    state.shopCart = {
      ...shopCart
    }
    // 将数据缓存到本地
    setLocalStore('shopCart', state.shopCart)
  },

  // 减少商品
  [REDUCE_GOODS] (state, { goodsID }) {
    const shopCart = state.shopCart
    const goods = shopCart[goodsID]
    // 商品数量> 0 就减减
    if (goods && goods.num > 0) {
      goods.num--
    }
    // 数量 = 0 就删除该属性
    if (goods.num === 0) {
      delete shopCart[goodsID]
    }
    // 同步购物车
    state.shopCart = {
      ...shopCart
    }
    // 同步本地数据
    setLocalStore('shopCart', state.shopCart)
  },
  // 选中单个商品
  [SINGLE_SELECT_GOODS] (state, { goodsID }) {
    // 取出state的商品
    const shopCart = state.shopCart
    const goods = shopCart[goodsID]
    goods && goods.checked ? goods.checked = !goods.checked : Vue.set(goods, 'checked', true)
    state.shopCart = {
      ...shopCart
    }
    setLocalStore('shopCart', state.shopCart)
  },

  // 全选商品， 传入一个isSelected
  [ALL_SELECT_GOODS] (state, { isCheckedAll }) {
    const shopCart = state.shopCart
    Object.values(shopCart).forEach(item => {
      item.checked ? item.checked = !isCheckedAll : Vue.set(item, 'checked', !isCheckedAll)
    })
    state.shopCart = {
      ...shopCart
    }
    setLocalStore('shopCart', state.shopCart)
  },

  // 删除商品
  [DELETE_SELECT_GOODS] (state) {
    const shopCart = state.shopCart
    Object.values(shopCart).forEach(item => {
      if (item.checked) {
        delete shopCart[item.id]
      }
    })
    state.shopCart = {
      ...shopCart
    }
    setLocalStore('shopCart', state.shopCart)
  },

  // 页面初始化获取本地购物车的数据
  // 初始化购物车信息
  [INIT_SHOP_CART] (state) {
    const initShopCart = getLocalStore('shopCart')
    // 如果购物车有数据那么就把它通过对象的方式赋值给store
    initShopCart && (state.shopCart = JSON.parse(initShopCart))
  },

  // 保存用户信息
  [USER_INFO] (state, { userInfo }) {
    state.userInfo = userInfo
    setLocalStore('userInfo', state.userInfo)
  },

  // 初始化用户信息
  [INIT_USER_INFO] (state) {
    const initUserInfo = getLocalStore('userInfo')
    if (initUserInfo) {
      state.userInfo = JSON.parse(initUserInfo)
    }
  },

  // 修改昵称
  [CHANGE_USER_NICK_NAME] (state, { nickName }) {
    const userInfo = state.userInfo
    Object.keys(userInfo).forEach(item => {
      if (item === userInfo.user_name) {
        userInfo[userInfo.user_name] = nickName
      }
    })
    state.userInfo = {
      ...userInfo
    }
    setLocalStore('userInfo', state.userInfo)
  },

  // 用户生日
  [USER_INFO_BRITHDAY] (state, { brithday }) {
    // 取出state中的用户信息
    const userInfo = state.userInfo
    Object.values(userInfo).forEach(info => {
      info.brithday ? info.brithday = brithday : Vue.set(userInfo, 'brithday', brithday)
    })
    state.userInfo = {
      ...userInfo
    }
    setLocalStore('userInfo', state.userInfo)
  },
  // 用户性别
  [USER_INFO_SEX] (state, { sex }) {
    const userInfo = state.userInfo
    Object.values(userInfo).forEach(info => {
      info.sex ? info.sex = sex : Vue.set(userInfo, 'sex', sex)
    })
    state.userInfo = {
      ...userInfo
    }
    setLocalStore('userInfo', state.userInfo)
  },

  // 退出登录
  [LOGIN_OUT] (state) {
    state.userInfo = {}
    state.shopCart = {}
    removeLocalStore('userInfo')
    removeLocalStore('shopCart')
    removeLocalStore('shippingAddress')
  },
  [INIT_USER_SHOPPING_ADDRESS] (state) {
    let initUserShoppingAddress = getLocalStore('userAddress')
    state.userAddress = JSON.parse(initUserShoppingAddress) || []
  },
  // 地址
  // 增加用户地址
  [ADD_USER_SHOPPING_ADDRESS] (state, { content }) {
    state.userAddress = [...state.userAddress, content]
    setLocalStore('userAddress', state.userAddress)
    console.log(state.userAddress)
  },
  // 删除用户地址
  [DELETE_USER_SHOPPING_ADDRESS] (state, { id }) {
    state.userAddress = state.userAddress.filter(item => item.id !== id)
    setLocalStore('userAddress', state.userAddress)
  },
  // 修改用户地址信息
  [CHANGE_USER_SHOPPING_ADDRESS] (state, { content }) {
    // 找到被修改地址的索引
    const index = state.userAddress.findIndex(item => item.id === content.id)
    state.userAddress.splice(index, 1, content)
  },

  // 添加商品进购物车
  [ADD_TO_CART] (state, goods) {
    // 判断用户是否登录
    if (state.userInfo.token) {
      setTimeout(() => {
        this.commit('ADD_GOODS', {
          goodsID: goods.id,
          goodsName: goods.name,
          smallImage: goods.small_image,
          goodsPrice: goods.price
        })
        Toast({
          message: '成功加入购物车',
          duration: 800
      })
      }, 900)
    } else {
      Router.push('/login')
    }
  }
}
